package com.lefevre.cms.service.membershipCard.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lefevre.cms.bean.QueryResult;
import com.lefevre.cms.bean.membershipCard.MembershipCardGiftItem;
import com.lefevre.cms.bean.membershipCard.MembershipCardGiftTask;
import com.lefevre.cms.bean.user.UserRoleGroup;
import com.lefevre.cms.mapper.MembershipCardGiftItemMapper;
import com.lefevre.cms.mapper.MembershipCardGiftTaskMapper;
import com.lefevre.cms.service.besa.DaoSupport;
import com.lefevre.cms.service.membershipCard.MembershipCardGiftTaskService;
import com.lefevre.cms.service.user.UserRoleService;
import com.lefevre.cms.web.action.SystemException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 会员卡赠送任务实现
 *
 * @author Lefevre
 */
@Service
@Transactional
public class MembershipCardGiftTaskImpl extends DaoSupport<MembershipCardGiftTask> implements MembershipCardGiftTaskService {
    private static final Logger logger = LoggerFactory.getLogger(MembershipCardGiftTaskImpl.class);
    @Resource
    UserRoleService userRoleService;
    @Resource
    private MembershipCardGiftTaskMapper baseMapper;
    @Resource
    private MembershipCardGiftItemMapper membershipCardGiftItemMapper;

    /**
     * 根据Id查询会员卡赠送任务
     *
     * @param membershipCardGiftTaskId 会员卡赠送任务Id
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public MembershipCardGiftTask findById(Long membershipCardGiftTaskId) {
        return baseMapper.selectById(membershipCardGiftTaskId);
    }

    /**
     * 查询启用的长期会员卡赠送任务
     *
     * @return
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    @Cacheable(value = "membershipCardGiftTaskBean_cache", key = "'default'")
    public List<MembershipCardGiftTask> findEnableMembershipCardGiftTask() {
        Date currentTime = new Date();
        return baseMapper.selectList(new LambdaQueryWrapper<MembershipCardGiftTask>()
                .le(MembershipCardGiftTask::getExpirationDate_start, currentTime)
                .ge(MembershipCardGiftTask::getExpirationDate_end, currentTime)
                .eq(MembershipCardGiftTask::getType, 10)
                .eq(MembershipCardGiftTask::isEnable, true));
    }


    /**
     * 保存会员卡赠送任务
     *
     * @param membershipCardGiftTask 会员卡赠送任务
     */
    @CacheEvict(value = "membershipCardGiftTaskBean_cache", allEntries = true)
    public void saveMembershipCardGiftTask(MembershipCardGiftTask membershipCardGiftTask) {
        baseMapper.insert(membershipCardGiftTask);
    }


    /**
     * 修改会员卡赠送任务
     *
     * @param membershipCardGiftTask 会员卡赠送任务
     * @return
     */
    @CacheEvict(value = "membershipCardGiftTaskBean_cache", allEntries = true)
    public Integer updateMembershipCardGiftTask(MembershipCardGiftTask membershipCardGiftTask) {
        return baseMapper.updateById(membershipCardGiftTask);
    }

    /**
     * 删除会员卡赠送任务
     *
     * @param membershipCardGiftTaskId 会员卡赠送任务Id
     */
    @CacheEvict(value = "membershipCardGiftTaskBean_cache", allEntries = true)
    public Integer deleteMembershipCardGiftTask(Long membershipCardGiftTaskId) {
        return baseMapper.deleteById(membershipCardGiftTaskId);
    }


    /**-------------------------------------------------------------------------------------------------------------**/

    /**
     * 保存会员卡赠送项
     *
     * @param membershipCardGiftItem 会员卡赠送项
     * @param add_userRoleGroup      增加用户角色组
     * @param update_userRoleGroup   修改用户角色组
     * @return
     */
    public void saveMembershipCardGiftItem(Object membershipCardGiftItem, UserRoleGroup add_userRoleGroup, UserRoleGroup update_userRoleGroup) {

        membershipCardGiftItemMapper.insert((MembershipCardGiftItem) membershipCardGiftItem);

        //角色组不存在时添加
        if (add_userRoleGroup != null) {
            List<UserRoleGroup> userRoleGroupList = new ArrayList<>();
            userRoleGroupList.add(add_userRoleGroup);
            userRoleService.saveUserRoleGroup(userRoleGroupList);
        }
        //角色组存在时更新
        if (update_userRoleGroup != null) {
            int i = userRoleService.updateUserRoleGroup(update_userRoleGroup.getUserRoleId(), update_userRoleGroup.getUserName(), update_userRoleGroup.getValidPeriodEnd());
            if (i == 0) {
                throw new SystemException("角色组更新错误");
            }
        }
    }


    /**
     * 根据会员卡赠送项Id查询会员卡赠送项
     *
     * @param membershipCardGiftItemId
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public MembershipCardGiftItem findMembershipCardGiftItemById(String membershipCardGiftItemId) {
        return membershipCardGiftItemMapper.selectById(membershipCardGiftItemId);
    }


    /**
     * 会员卡赠送项分页
     *
     * @param membershipCardGiftTaskId 会员卡赠送任务Id
     * @param firstIndex               索引开始,即从哪条记录开始
     * @param maxResult                获取多少条数据
     */
    @Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
    public QueryResult<MembershipCardGiftItem> findMembershipCardGiftItemPage(Long membershipCardGiftTaskId, int firstIndex, int maxResult) {

        QueryResult<MembershipCardGiftItem> qr = new QueryResult<>();

        LambdaQueryWrapper<MembershipCardGiftItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MembershipCardGiftItem::getMembershipCardGiftTaskId, membershipCardGiftTaskId);
        Page<MembershipCardGiftItem> membershipCardGiftItemPage = membershipCardGiftItemMapper.selectPage(new Page<>(firstIndex, maxResult), wrapper);

        qr.setResultlist(membershipCardGiftItemPage.getRecords());
        qr.setTotalrecord(membershipCardGiftItemPage.getTotal());

        return qr;
    }


}
